package com.wzx.ptplatform.controller.company;

import com.wzx.ptplatform.domain.PartTimeMessage;
import com.wzx.ptplatform.domain.WorkDetail;
import com.wzx.ptplatform.service.PartTimeMessageService;
import com.wzx.ptplatform.service.WorkDetailService;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "企业模块")
@RestController
public class CompanyIndexController {

    final PartTimeMessageService partTimeMessageService;
    final WorkDetailService workDetailService;

    public CompanyIndexController(PartTimeMessageService partTimeMessageService, WorkDetailService workDetailService) {
        this.partTimeMessageService = partTimeMessageService;
        this.workDetailService = workDetailService;
    }

    @GetMapping("company_index")
    public ModelAndView companyIndex() {
        return new ModelAndView("company/index");
    }

    @GetMapping("add_pt_message")
    public ModelAndView addPtMessage(String id, Model model) {
        model.addAttribute("id", id);
        return new ModelAndView("company/add_pt_message");
    }

    @PostMapping("company_add_pt_message_save_or_edit")
    public Map<String, Object> companyAddPtMessageSaveOrEdit(PartTimeMessage partTimeMessage) {
        Map<String, Object> map = MapUtils.initializeMap();
        partTimeMessage.setPostStatus("0");
        if (partTimeMessage.getWorkDetail().getDuty() != null || partTimeMessage.getWorkDetail().getRequestMessage() != null ||
                partTimeMessage.getWorkDetail().getJoinMessage() != null || partTimeMessage.getWorkDetail().getContact() != null ||
                partTimeMessage.getWorkDetail().getSalary() != null) {
            WorkDetail workDetail = new WorkDetail(partTimeMessage.getWorkDetail().getSalary(), partTimeMessage.getWorkDetail().getDuty(), partTimeMessage.getWorkDetail().getRequestMessage(), partTimeMessage.getWorkDetail().getJoinMessage(), partTimeMessage.getWorkDetail().getContact());
            workDetailService.saveOrEdit(workDetail);
        }
        WorkDetail workDetail = workDetailService.findByDutyAndSalary(partTimeMessage.getWorkDetail().getDuty(), partTimeMessage.getWorkDetail().getSalary());
        partTimeMessage.setWorkDetail(workDetail);
        if (partTimeMessageService.saveOrEdit(partTimeMessage)) {
            MapUtils.successMap(map);
        }
        return map;
    }
}
